var main_api = {
    home_config_params:{
        homeNav:1, //头部导航:1(显示)，0(隐藏)
        homeFooter:0, //底部导航:1(显示)，0(隐藏)
    },
    // 头部导航
    loadCommonHeader:function(data){
        if(main_api.home_config_params.homeNav){
            this.useTemplate('/wx/html/header.html',data).then(function(res){
                $('body').prepend(res)
            })
        }
    },
    loadCommonFooter:function(){
        var data = [{'title':'首页','icon':'/wx/static/images/home/footer_index0.png','link':'/wx/html/home/index.html','link_mini':'/pages/index/index','flagClass':'index'},
            {'title':'圈子','icon':'/wx/images/home/footer_circle0.png','link':'/wx/html/home/forum.html','link_mini':'/pages/forum/forumList/index','flagClass':'circle'},
            {'title':'学习','icon':'/wx/images/home/footer_course_list0.png','link':'/wx/html/home/course_list.html','link_mini':'/pages/course/courseList/index','flagClass':'course_list'},
            {'title':'我的','icon':'/wx/images/home/footer_user_center0.png','link':'/wx/html/home/user_center.html','link_mini':'/pages/user/center/index','flagClass':'user_center'}
        ]
        if(main_api.home_config_params.homeFooter){
            this.useTemplate('/wx/html/footer.html',{'data':data}).then(function(res){
                $('body').append(res)
            }).then(function(){
                var select = $('body').attr('data-footer-locate')
                $('.js_home_footer .weui-tabbar__item.' + select).addClass('weui-bar__item--on').find('.weui-tabbar__icon img').attr('src','/wx/images/home/footer_' + select + '1.png')

                // 小程序底部导航的跳转
                $('.js_link_to').on('click',function(){
                    var link = $(this).attr('data-link'),
                    link_mini = $(this).attr('data-link-mini');
                    if(helper.get_platform() == 4){
                        wx.miniProgram.redirectTo({
                            url: link_mini
                        })
                    }else{
                        window.location.href = link
                    }
                })
            })
        }
    },
    // template 引入外部模板
    useTemplate:function(sourceUrl,data){
        return new Promise(function(resolve,reject){
            helper.xhr_get(sourceUrl,function(res){
                var render = template.compile(res),
                html = render(data);
                // document.getElementById(dom).innerHTML = html
                resolve(html)
            })
        })
    },
    /**
     * [verifyNumber 验证是否为数字]
     * @param  {[type]}       num [description]
     * @return {[Boolean]}    [true:表示是数字；false:表示不是数字]
     */
    verifyNumber:function(num){
        var reg =  /^[0-9]+.?[0-9]*$/;
        if(reg.test(num)){
            return true;
        }
        return false;
    },
    // 验证真实姓名
    verifyRealName:function(name){
        var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
        if(reg.test(name)){
            return true;
        }
        return false;
    },
    /**
     * [countTime 活动倒计时]
     * @param  {[type]} endtime [活动结束时间]
     * @param  {[type]} type    [活动id]
     * @return {[type]}         [description]
     */
    countTime:function(endtime,type){
        var d=0,
            h=0,
            m=0,
            s=0,
        timestamp = endtime - new Date().getTime()/1000,
        timer = setInterval(function(){
            if(timestamp>=0){
                d = checkTime(parseInt(timestamp/(60*60*24))); //天数
                h = checkTime(parseInt(timestamp/(60*60)%24)); //小时
                m = checkTime(parseInt(timestamp/60 % 60)); //分钟
                s = checkTime(parseInt(timestamp % 60)); //秒
            }
            $(".js_time_d"+type).text(d);
            $(".js_time_h"+type).text(h)
            $(".js_time_m"+type).text(m)
            $(".js_time_s"+type).text(s)
            timestamp--;
        },1000)
        if(timestamp<0){
            clearInterval(timer);
        }
        function checkTime(i) { //将0-9的数字前面加上0
            if (i < 10) {
                i = "0" + i;
            }
                return i;
        }
    },
    // 登陆回跳
    gotoLoginPage: function() {
        location.href = '/wx/html/home/user_login.html?topage=' + encodeURIComponent(location.href);
    },
    /**
     * [getJobType 职业类型]
     * @param  {string} flag       [name->取名称；unit->取价格单位；list->取列表]
     * @param  {String} type_num   [职业类型的id]
     * @return {[type]}            [description]
     */
    getJobType:function(flag,type_num=''){
        // [1高级月嫂,2高级育婴师,3高级催乳师,4高级产后康复师,5高级早教师]
        var list = {
            1:{'name':'高级月嫂','unit':'元/26天'},
            2:{'name':'高级育婴师','unit':'元/月'},
            3:{'name':'高级催乳师','unit':'元/次'},
            4:{'name':'高级产后康复师','unit':'元/次'},
            5:{'name':'高级早教师','unit':'元/课时'}
        }
        if(flag == 'name'){
            return list[type_num].name
        }else if(flag == 'unit'){
            return list[type_num].unit
        }else if(flag == 'list'){
            var arr = []
            for(var k in list){
                var a = {}
                a.name = list[k].name
                a.id = k
                arr.push(a)
            }
            return arr
        }
    },
    /**
     * [getOrderStatus 判断抢单是否已结束]
     * @param  {[type]} finish_timestamp [抢单结束时间]
     * @return {[type]}                  [true->未结束；false->已结束]
     */
    getRobOrderStatus:function(finish_timestamp){
        var now_time = Number(new Date().getTime()/1000) //当前时间戳[秒数]
        if(now_time < Number(finish_timestamp)){
            return true
        }else{
            return false
        }
    },
    /**
     * [getRobType 抢单类型]
     * @param  {string} flag       [name->取名称；unit->取价格单位；list->取列表]
     * @param  {String} type_num   [抢单类型的id]
     * @return {[type]}            [description]
     */
    getRobType:function(flag,type_num=''){
        // 抢单类型type [1月嫂单2育婴师单3催乳单4产后康复]
        var list = {
            1:{'name':'月嫂单','unit':'12'},
            2:{'name':'育婴师单','unit':'22'},
            3:{'name':'催乳单','unit':'32'},
            4:{'name':'产后康复','unit':'42'}
        }
        if(flag == 'name'){
            return list[type_num].name
        }else if(flag == 'unit'){
            return list[type_num].unit
        }else if(flag == 'list'){
            var arr = []
            for(var k in list){
                var a = {}
                a.name = list[k].name
                a.id = k
                arr.push(a)
            }
            return arr
        }
    },
    /**
     * [getNation 民族]
     * @return {[type]} [description]
     */
    getNation:function(){
        var list = ["汉族", "壮族", "满族", "回族", "苗族", "维吾尔族", "土家族", "彝族", "蒙古族", "藏族", "布依族", "侗族", "瑶族", "朝鲜族", "白族", "哈尼族",
        "哈萨克族", "黎族", "傣族", "畲族", "傈僳族", "仡佬族", "东乡族", "高山族", "拉祜族", "水族", "佤族", "纳西族", "羌族", "土族", "仫佬族", "锡伯族",
        "柯尔克孜族", "达斡尔族", "景颇族", "毛南族", "撒拉族", "布朗族", "塔吉克族", "阿昌族", "普米族", "鄂温克族", "怒族", "京族", "基诺族", "德昂族", "保安族",
        "俄罗斯族", "裕固族", "乌孜别克族", "门巴族", "鄂伦春族", "独龙族", "塔塔尔族", "赫哲族", "珞巴族"];
        return list
    },
    /**
     * [getLessonLength 课程时长]
     * @param  {[string]} timestamp [时长时间戳【秒】]
     * @return {[type]}             [时分秒格式，如 10:02:30]
     */
    getLessonLength:function(timestamp){
        var timestamp = Number(timestamp),
        hours = parseInt(timestamp/3600),
        minutes = parseInt((timestamp%3600)/60),
        seconds = (timestamp%3600)%60
        //
        function format(num){
            var num = Number(num)
            if(num <= 9){
                return num = "0" + num;
            }else{
                return num
            }
        }
        return format(hours) + ":" + format(minutes) + ":" + format(seconds)
    }
}
var init = (function(){
    helper.android_fit('device-width')
    // 加载公共配置参数
    var loadParams = (function(){
        if(typeof config_autoload != 'undefined'){
            main_api.home_config_params = helper.array_merge(main_api.home_config_params, config_autoload)
        }
        // 加载公共底部
        main_api.loadCommonFooter()
    })(loadParams)

    template.helper("main_api", function(timestamp,type) {
        return main_api;
    });
    // 默认图片
    template.helper('getDefaultPic',function(url){
        var urlStr = "";
        if (url != "") {
            urlStr = url;
            return urlStr;
        } else {
            urlStr = "/wx/images/home/index_subnav_icon3.png";
            return urlStr;
        }
    })
    // 证书分类
    template.helper('getCatidName',function(catid){
        // 证书分类：[1身份证2健康证3母婴护理证4育婴师证5催乳师证6营养师证7早教师证8产康师9培训证书10学历证书]
        var cat = {
            1:"身份证",
            2:"健康证",
            3:"母婴护理证",
            4:"育婴师证",
            5:"催乳师证",
            6:"营养师证",
            7:"早教师证",
            8:"产康师证",
            9:"培训证书",
            10:"学历证书"
        };
        return cat[catid];
    })
    // 职业类型
    template.helper("getJobType",function(type_num,flag){
        return main_api.getJobType(type_num,flag)
    })
})(init)
